package service;

import dao.FoodDao;
import dao.OrderDao;
import dao.OrderFoodBkDao;
import dao.impl.FoodDaoImpl;
import dao.impl.OrderDaoImpl;
import dao.impl.OrderFoodBkDaoImpl;
import pojo.Food;
import pojo.Order;
import pojo.OrderFood;
import util.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

public class BusinessService {

    private OrderDao orderDao = new OrderDaoImpl();
    private OrderFoodBkDao orderFoodBkDao = new OrderFoodBkDaoImpl();
    private FoodDao foodDao = new FoodDaoImpl();

    /**
     * 查询营业额
     *
     * @param request
     * @param response
     */
    public void findOrderMoneyByDay(HttpServletRequest request, HttpServletResponse response) {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();

        List<String> dayList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance();   //得到日历
            calendar.setTime(dNow);//把当前时间赋给日历
            calendar.add(Calendar.DAY_OF_MONTH, -i);  //设置为前i天
            dBefore = calendar.getTime();   //得到前一天的时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
            String defaultStartDate = sdf.format(dBefore);    //格式化前一天
            dayList.add(defaultStartDate);
        }
        List<String> moneyList = new ArrayList<>();
        for (String day : dayList) {
            String orderMoneyByDay = orderDao.findOrderMoneyByDay(day);
            if (orderMoneyByDay == null) {
                orderMoneyByDay = "0";
            }
            moneyList.add(orderMoneyByDay);
        }
        for (int i = 0; i < dayList.size(); i++) {
            dayList.set(i, dayList.get(i).substring(dayList.get(i).length() - 2));
        }
        dayList.addAll(moneyList);
        Result.response(200, "获取日期成功！", response, dayList);

    }

    /**
     * 获取今日的订单数量
     *
     * @param request
     * @param response
     */
    public void findOrderCountByDay(HttpServletRequest request, HttpServletResponse response) {
        Date dNow = new Date();   //当前时间
        Calendar calendar = Calendar.getInstance();   //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String today = sdf.format(dNow);
        List<Order> orderList = orderDao.findOrderByDay(today);
        Integer size;
        if (orderList == null) {
            Result.response(200, "查询成功！", response, 0);
            return;
        }
        Result.response(200, "查询成功！", response, orderList.size());
    }

    /**
     * 获取今日的订单数量
     *
     * @param request
     * @param response
     */
    public void findPopularFood(HttpServletRequest request, HttpServletResponse response) {
        Date dNow = new Date();   //当前时间
        Calendar calendar = Calendar.getInstance();   //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String today = sdf.format(dNow);
        List<Order> orderList = orderDao.findOrderByDay(today);
        if (orderList == null || orderList.size() == 0) {
            Result.response(1001, "还没有产生今日热门！", response, 0);
            return;
        }
        // 获取今日订单的id的最大值和最小值
        String minId = orderList.get(0).getOrderId().toString();
        String maxId = orderList.get(orderList.size() - 1).getOrderId().toString();
        OrderFood popularFood = orderFoodBkDao.findPopularFood(minId, maxId);
        Integer sumCountById = orderFoodBkDao.findSumCountById(minId, maxId, popularFood.getFoodId().toString());
        Food foodById = foodDao.findFoodById(popularFood.getFoodId().toString());
        Result.response(200, "查找成功！", sumCountById, response, foodById);
    }

    /**
     * 获取累计销售热门的食品
     *
     * @param request
     * @param response
     */
    public void findAllPopularFood(HttpServletRequest request, HttpServletResponse response) {
        List<Food> foodList = orderFoodBkDao.findSumCountFoodId();
        List<String> sumCountList = orderFoodBkDao.findSumCount();
        List<Food> resultFoodList = new ArrayList<>();
        for (int i = 0; i < foodList.size(); i++) {
            Food foodById = foodDao.findFoodById(foodList.get(i).getFoodId().toString());
            foodById.setPage(Integer.valueOf(sumCountList.get(i)));
            resultFoodList.add(foodById);
        }
        Result.response(200, "获取成功！", response, resultFoodList);
    }

    public void find4PopularFood(HttpServletRequest request, HttpServletResponse response) {
        List<Food> foodList = orderFoodBkDao.findSumCountFoodId();
        List<String> sumCountList = orderFoodBkDao.findSumCount();
        List<Food> resultFoodList = new ArrayList<>();
        if (foodList.size() != 0) {
            for (int i = 0; i < 5; i++) {
                Food foodById = foodDao.findFoodById(foodList.get(i).getFoodId().toString());
                foodById.setPage(Integer.valueOf(sumCountList.get(i)));
                resultFoodList.add(foodById);
            }
        }
        Result.response(200, "获取成功！", response, resultFoodList);
    }
}
